package com.t.bricks.bussiness.db.dao.ordersreturned.impl;

import java.util.Arrays;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import com.t.bricks.bussiness.db.dao.ordersreturned.OrdersReturnedDao;
import com.t.bricks.bussiness.db.entity.ordersreturned.OrdersReturned;
import com.t.bricks.bussiness.db.mapper.ordersreturned.OrdersReturnedMapper;
import com.t.bricks.model.Log;
import com.t.bricks.model.MsgEmity;

@Repository
public class OrdersReturnedDaoImpl implements OrdersReturnedDao<OrdersReturned> {

	@Autowired
	private OrdersReturnedMapper ordersReturnedMapper;

	/**
	 * 添加
	 * @Param sId
	 * @Param sOrderId
	 * @Param sCreator
	 * @Param sMemo
	 * @return MsgEmity
	 */
	@Override
	@Transactional
	public MsgEmity addCustom(String sId, String sOrderId, String sCreator, String sMemo) {
		try {
			int i = ordersReturnedMapper.addCustom(sId, sOrderId, sCreator, sMemo);
			if (i < 1) {
				Log.error("新增未能得到预期影响值(预期1):", i);
				return new MsgEmity(false, "新增数据失败！", 7001);
			}
			
			i = ordersReturnedMapper.changeOrderState(sOrderId, 7);
			if (i < 1) {
				Log.error("修改订单表Orders状态值未能得到预期影响值(预期1):", i);
				TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();//手动回滚事务
				return new MsgEmity(false, "新增数据失败,事务修改订单表Orders状态值未能得到预期影响值(预期1)！", 7002);
			}
		} catch (Exception e) {
			Log.error("新增时发生异常:", e.getMessage());
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();//手动回滚事务
			return new MsgEmity(false, "新增失败,请检查异常日志！", 7003);
		}
		
		return new MsgEmity(true, "新增成功！", 7999);
	}
	
	/**
	 * 修改状态
	 * @param sId 编号
	 * @param iState 状态值
	 * @param iVersion 记录版本号
	 * @param sCreator 创建人
	 * @param sOrderId 订单编号
	 * @param iOrderState 待影响的订单状态,若为-1则不处理
	 * @return MsgEmity 返回执行情况
	 */
	@Override
	@Transactional
	public MsgEmity changeState(String sId, int iState, int iVersion,
			String sCreator, String sOrderId, int iOrderState) {
		try {
			int i = ordersReturnedMapper.changeState(sId, iState, iVersion);
			if (i < 1) {
				List<String> fieldNames = Arrays.asList("iState", "iVersion");
				MsgEmity me = this.getValueByFieldName(sId, fieldNames, false);
				if (!me.isSuccess()) {
					return me.setData((Integer)me.getData() - 1000);
				}
				
				OrdersReturned ordersReturned = me.getData();//如果影响数小于1,则检查数据
				if (null == ordersReturned) {
					return new MsgEmity(false, "没有找到对应数据！", 7001);//删除影响数失败,但数据也不存在则视为成功
				}
				
				if (iVersion != ordersReturned.getiVersion().intValue()) {
					return new MsgEmity(false, "更新状态失败，系统中的数据可能已经被更新！", 7002);
				}
				
				if (iState >= 0 && iState < ordersReturned.getiState()) {
					return new MsgEmity(false, "更新状态失败，状态禁止逆操作！", 7003);
				}

				Log.error("更新状态未能得到预期影响值(预期1):", i);
				return new MsgEmity(false, "更新状态数据失败！", 7004);
			}

			i = ordersReturnedMapper.changeOrderState(sOrderId, iOrderState);
			if (i < 1) {
				Log.error("新增未能得到预期影响值(预期1):", i);
				TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();//手动回滚事务
				return new MsgEmity(false, "新增数据失败！", 7001);
			}
		} catch (Exception e) {
			Log.error("更新状态时发生异常:", e.getMessage());
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();//手动回滚事务
			return new MsgEmity(false, "更新状态失败！", 7005);
		}
		
		return new MsgEmity(true, "更新状态成功！", 7999);
	}

}
